package cn.onlineclass.service;

import cn.onlineclass.dao.BookMapper;
import cn.onlineclass.entity.OBook;
import cn.onlineclass.util.MybatisUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class BookService {
    public static final BookService INST=new BookService();
    public AjaxResult update(OBook record){
        SqlSession session = MybatisUtil.get();
        try {
            BookMapper mapper = session.getMapper(BookMapper.class);
            int update = mapper.updateById(record);
            return AjaxResult.success(update);
        }finally {
            MybatisUtil.close(session);
        }
    }

    public AjaxResult insertOBook(OBook record){
        SqlSession session = MybatisUtil.get();
        try {
            BookMapper mapper = session.getMapper(BookMapper.class);
            int add = mapper.insertOBook(record);
            return AjaxResult.success(add);
        }finally {
            MybatisUtil.close(session);
        }
    }

    public AjaxResult del(long id){
        SqlSession session = MybatisUtil.get();
        try {
            BookMapper mapper = session.getMapper(BookMapper.class);
            int del = mapper.deleteById(id);
            return AjaxResult.success(del);
        }finally {
            MybatisUtil.close(session);
        }
    }

//    public AjaxPage search(int pageNo, int pageSize, String name, long unitid){
//        SqlSession session = MybatisUtil.get();
//        try {
//            List<Book> search = ;
//            BookMapper mapper = session.getMapper(BookMapper.class);
//            int total
//            return mapper.searchAll(pageNo,pageSize,name,unitid);
//        }finally {
//            MybatisUtil.close(session);
//        }
//    }
public AjaxPage searchAll(int pageNo, int pageSize){
    SqlSession s= MybatisUtil.get();
    try {
        BookMapper mapper = s.getMapper(BookMapper.class);
        List<OBook> search = mapper.searchAll(pageNo, pageSize);
        return new AjaxPage(search,10,0,pageNo);
    }finally {
        MybatisUtil.close(s);
    }
}


    public AjaxResult searchTotal(String name,long id){
        SqlSession session = MybatisUtil.get();
        try {
            BookMapper mapper = session.getMapper(BookMapper.class);
            int searchtotal = mapper.searchTotal(name,id);
            return AjaxResult.success(searchtotal);
        }finally {
            MybatisUtil.close(session);
        }
    }
}
